package org.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.example.config.thread.AfterCommitExecutor;
import org.example.entity.Worker;
import org.example.entity.WorkerFunction;
import org.example.mapper.WorkerMapper;
import org.example.service.WorkerFunctionService;
import org.example.service.WorkerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper, Worker> implements WorkerService {

    @Resource
    private AfterCommitExecutor afterCommitExecutor;

    @Resource
    private WorkerFunctionService workerFunctionService;
    private AtomicInteger atomicInteger = new AtomicInteger();

    private AtomicInteger updateIndex = new AtomicInteger();

    @Override
    public void translateSave(Map<String, Object> params) {

        afterCommitExecutor.execute(()->{
            Object name = params.get("name");
            List<Worker> workers = lambdaQuery().eq(Worker::getName, name)
                    .list();
            int andDecrement = atomicInteger.getAndIncrement();
            workers.forEach(worker -> worker.setRemark(String.valueOf(andDecrement)));
            updateBatchById(workers);
            String jsonString = JSONObject.toJSONString(workers);
            log.info("查询第 {} 次结果 : {}",andDecrement,jsonString);

        });
    }

    @Override
    public void translateUpdate(Map<String, Object> params) {

        afterCommitExecutor.execute(()->{
            Object id = params.get("id");
            Worker one = lambdaQuery().eq(Worker::getId, id)
                    .one();
            int andIncrement = updateIndex.getAndIncrement();
            log.info("第 {} 次 查询修改结果 :{}",andIncrement,JSONObject.toJSONString(one));
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDel() {
        List<Worker> list = list();
        WorkerMapper baseMapper = getBaseMapper();
        int i = baseMapper.deleteBatchIds(list.stream().map(Worker::getId).collect(Collectors.toList()));
//        return i > 0;
//        list.stream().findFirst().ifPresent(this::removeById);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveEntity() {
        Worker worker = new Worker();
        worker.setName("单个测试")
                .setScene("单个场景")
                .setAge(21)
                .setDoes("努力搞钱");
        int flag = baseMapper.insert(worker);

        Random random = new Random();

        int num = random.nextInt(10);

        List<WorkerFunction> workerFunctions = Lists.newArrayList();

        for (int i = 0; i < num; i++) {
            WorkerFunction workerFunction = new WorkerFunction();
            workerFunction.setFunction("功能" + i);
            workerFunction.setWorkerId(worker.getId());
            workerFunctions.add(workerFunction);
        }

        if(CollectionUtil.isNotEmpty(workerFunctions)){
            workerFunctionService.saveBatch(workerFunctions);
        }
        return flag;
    }
}
